﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Impls;
using Microhard.WeixinMP.Interfaces;
using Microhard.WeixinMP.Models;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core;

namespace Micrhard.FMCG.Server.Weixin
{
    public class CoinExchange : BaseResponseImpl
    {
        public override Microhard.WeixinMP.Interfaces.WeixinCommand Command
        {
            get
            {
                WeixinCommand wc = new WeixinCommand();
                wc.Event = "CLICK";
                wc.EventKey = "myCoin";
                return wc;
            }
        }

        protected override Microhard.WeixinMP.Models.WeixinBaseData<object> CreateFeatureResponse(Microhard.WeixinMP.Models.RequestServiceModel request, Microhard.WeixinMP.Models.WeixinBaseData<object> response)
        {
            //创建会话成功，发起服务
            response.MsgType = WeixinMsgType.Text;
            response.FuncFlag = WeixinMsgType.FuncFlag_False;
            WeixinTxt result = new WeixinTxt();
            ICoinsMng icm = IoC.Resolve<ICoinsMng>();
            var m = icm.GetCustomerCoinByOpenID(response.ToUserName, request.WXMPAccountUser.CompanyID);
            StringBuilder msg = new StringBuilder();
            msg.AppendLine("您的剩余积分:" + m.Coins);
            msg.AppendLine("历史兑换记录:");
            if (m.HistoryOrders != null && m.HistoryOrders.Count > 0)
            {
                foreach (var o in m.HistoryOrders)
                {
                    StringBuilder prdNames = new StringBuilder();
                    foreach (var d in o.Order.Detials)
                    {
                        prdNames.Append(d.PrdName);
                        prdNames.Append(",");
                    }
                    msg.AppendLine("产品名称:" + prdNames.ToString() + " 积分:" + o.CostCoins + " 兑换日期:" + o.Order.CreateTime.ToString("yyyy-MM-dd"));
                }
            }
            else
            {
                msg.AppendLine("无");
            }
            result.Content = msg.ToString();
            response.Content = result;
            return response;
        }
    }
}
